package ldh.im.fxgui.component;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import ldh.im.fxbase.util.MovableHelper;
import ldh.im.fxbase.util.ResizeHelper;
import ldh.im.fxgui.controller.StageControlController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class ImStage2 extends StackPane {

    private static final Logger LOGGER = LoggerFactory.getLogger(ImStage2.class);

    @FXML private Pane contentPane;
    @FXML private Label titleLabel;
    @FXML private Pane movablePane;
    @FXML private Pane headerPane;
    @FXML private StageControlController stagePaneController;

    private ObjectProperty<ImageView> stageIconProperty = new SimpleObjectProperty<>();
    private ObjectProperty<Node> contentProperty = new SimpleObjectProperty<>();

    private Stage newStage;
    private Scene scene;
    private double width = -1;
    private double height = -1;

    public ImStage2(double width, double height) {
        this.width = width;
        this.height = height;
        loadFxml();
    }

    public ImStage2() {
        loadFxml();
    }

    public Scene getCurrentScene() {
        return scene;
    }

    public Stage getStage() {
        return newStage;
    }

    public void show() {
        newStage.show();
        newStage.toFront();
    }

    public void centerOnScreen() {
        newStage.centerOnScreen();
    }

    public void close() {
        newStage.close();
        newStage = null;
    }

    public void setOnCloseRequest(EventHandler<WindowEvent> value){
        newStage.setOnCloseRequest(value);
    }

    public void setTitle(String title) {
        titleLabel.setText(title);
    }

    public ImageView getStageIconProperty() {
        return stageIconProperty.get();
    }

    public ObjectProperty<ImageView> stageIconPropertyProperty() {
        return stageIconProperty;
    }

    public void setStageIconProperty(ImageView stageIconProperty) {
        this.stageIconProperty.set(stageIconProperty);
    }

    public Node getContentProperty() {
        return contentProperty.get();
    }

    public ObjectProperty<Node> contentPropertyProperty() {
        return contentProperty;
    }

    public void setContent(Node contentPane) {
        this.contentProperty.set(contentPane);
    }

    private void initStage() {
        newStage = new Stage();
        newStage.initStyle(StageStyle.TRANSPARENT);

        if (width > 0 && height > 0) {
            scene = new Scene(this, width, height);
        } else {
            scene = new Scene(this);
        }
        LOGGER.debug("newStage scene {}, stage: {}", scene, newStage);

        scene.setFill(null);
        scene.getStylesheets().add(this.getClass().getResource("/css/var.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/stage.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/scrollbar.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/bootstrapfx.css").toExternalForm());
        newStage.setScene(scene);
        MovableHelper.movable(movablePane);

        stagePaneController.setStage(newStage);

        ResizeHelper.addResizeListener(newStage);
    }

    private void bindEvent() {
        stageIconProperty.addListener((InvalidationListener)->{
            if (stageIconProperty.get() != null) {
                if (headerPane.getChildren().size() > 3) {
                    headerPane.getChildren().remove(0);
                }
                headerPane.getChildren().add(0, stageIconProperty.get());
            }
        });

        contentProperty.addListener(l->{
            if (contentProperty.get() != null) {
                if (contentPane.getChildren().size() > 1) {
                    contentPane.getChildren().remove(1);
                }
                VBox.setVgrow(contentProperty.get(), Priority.ALWAYS);
                contentPane.getChildren().add(contentProperty.get());
            }
        });
    }

    private void loadFxml() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/ImStagePage.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        initStage();
        bindEvent();
    }
}
